package de.hdu.myjobapp.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.myjobapp.model.Application;
import de.hdu.myjobapp.model.Rating;
import de.hdu.myjobapp.model.Status;
import de.hdu.myjobapp.view.MyJobApplication;

public class StatusDao extends HibernateDaoSupport{
	//create a new Status
	public void create(Integer statusType, Date statusDate, Application applicationId, String statusDescription, Date statusDatenew){
		Status s = new Status();
		s.setStatusType(statusType);
		s.setStatusDate(statusDate);
		s.setApplicationId(applicationId);
		s.setStatusDescription(statusDescription);
		s.setStatusDatenew(statusDatenew);

		save(s);
	}

	//Saves the new recieved Status from Method create with hibernate into database
	public Status save(Status status){
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(status);
		return status;
	}

	//return all existing Status
	public List<Status> findAll(){
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Status.class);
	}
	//Return an existing Rating by naming ID
	public Status findById(int id){
		HibernateTemplate template = getHibernateTemplate();
		return(Status) template.get(Status.class, id);
	}
	/**
	 * Find all states related to a specific Application
	 * TODO - improve process not to read whole sql table
	 * @param Application Object to which are related the Status Objects
	 */
	public List<Status> findAll(Application application){
		HibernateTemplate template = getHibernateTemplate();
		List<Status> applicationStates = new ArrayList<Status>();
		List<Status> allstates = template.loadAll(Status.class);
		for(Status status:allstates){
			if(status.getApplicationId().getApplicationId().equals(application.getApplicationId())){
				MyJobApplication.getProject().getMainWindow().showNotification(application.getApplicationPosition());	
				applicationStates.add(status);
			}
			
		}
		return applicationStates;
	}

}


